﻿using CMS.Models;
using CMS.ViewModel;
using LiveCharts;
using LiveCharts.Defaults;
using LiveCharts.Wpf;
using Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CMS.View
{
    /// <summary>
    /// IndexView.xaml 的交互逻辑
    /// </summary>
    public partial class IndexView : UserControl
    {
        public IndexView()
        {
            InitializeComponent();
            Loaded += (s, e) =>
            {
                var vm = DataContext as IndexViewModel;
                var cargoProvider = new CargoProvider();
                var recordProvider = new RecordProvider();
                var cargos = cargoProvider.Select();
                var records = recordProvider.Select();
                vm.CargoLabels.Clear();
                vm.CargoChartValues.Clear();
                vm.PieSeries.Clear();

                // 显示物资的总量报表
                cargos.ForEach(cargo =>
                {
                    var list = records.FindAll(item => item.CargoId == cargo.Id);
                    if (list != null)
                    {
                        var inputCount = list.FindAll(item => item.RecordType == true).Sum(item => item.Number);
                        var outputCount = list.FindAll(item => item.RecordType == false).Sum(item => item.Number);
                        var sumCount = inputCount - outputCount;
                        cargo.Sum = sumCount;
                        vm.CargoLabels.Add(cargo.Name);
                        vm.CargoChartValues.Add(cargo.Sum);
                        vm.PieSeries.Add(
                            new PieSeries()
                            {
                                Title = cargo.Name,
                                Values = new ChartValues<double>
                                {
                                    cargo.Sum
                                }
                            });
                    }
                });

                // 显示用户流水报表
                var memberProvider = new MemberProvider();
                var members = memberProvider.Select();
                vm.MemberLabels.Clear();
                vm.MemberChartValues.Clear();
                members.ForEach(member =>
                {
                    int count = records.FindAll(record => record.MemberId == member.Id).Count;
                    vm.MemberLabels.Add(member.Name);
                    vm.MemberChartValues.Add(count);
                });


                foreach (var cargo in cargos)
                {

                    var list = records.FindAll(record => record.CargoId == cargo.Id);
                    var serise = new LineSeries();
                    serise.Title = cargo.Name;
                    serise.Values = new ChartValues<double>();
                    Axis axis = new Axis();
                    axis.ShowLabels = true;
                    axis.Labels = new List<string>();
                    list.ForEach(record =>
                    {
                        axis.Labels.Add(record.InsertDate.ToString());
                        serise.Values.Add(record.Number);
                    });
                    vm.LineSeries.Add(serise);
                    vm.LineAxisX.Add(axis);
                    break;
                }

                return;
                // 显示每个物资的折线图 
                cargos.ForEach(cargo =>
                {
                    var list = records.FindAll(item => item.CargoId == cargo.Id);
                    var serise = new LineSeries();
                    serise.Title = cargo.Name;
                    serise.Values = new ChartValues<double>();
                    Axis axis = new Axis();
                    axis.ShowLabels = true;
                    axis.Labels = new List<string>();
                    list.ForEach(record =>
                    {
                        axis.Labels.Add(cargo.InsertDate.ToString());
                        serise.Values.Add(record.Number);
                        //vm.LineSeries.Add(new LineSeries() { Title = cargo.Name, Values = new ChartValues<ObservableValue> { new ObservableValue(record.Number) } });
                    });
                    vm.LineSeries.Add(serise);
                    vm.LineAxisX.Add(axis);
                });

            };
        }
    }
}
